package com.testing.end_imageloader_demo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainActivity extends AppCompatActivity {

    private DisplayImageOptions op;
    private ImageView im;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 显示图片的配置
        op = new DisplayImageOptions.Builder()
                .cacheOnDisk(true)
                .cacheInMemory(true)
                //       .showImageOnLoading(R.drawable.ic_stub)// 图片正在下载时显示的图片
                //       .showImageForEmptyUri(R.drawable.ic_empty)// 请求的资源不存在时显示的图
                //       .showImageOnFail(R.drawable.ic_error)// 加载出错时显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565)
                //       .displayer(new RoundedBitmapDisplayer(500))// 给图片设置圆角 int值 表示
                .build();

        //找控件
        im = (ImageView) findViewById(R.id.im);
        Button but = (Button) findViewById(R.id.but);

        //url
        String str = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1488724020677&di=8b182a74cc57a29df6ebb6146c06cce2&imgtype=0&src=http%3A%2F%2Fimgsrc.baidu.com%2Fbaike%2Fpic%2Fitem%2F5fdf8db1cb134954a4d833a0534e9258d0094a34.jpg";
        //设置图片
        ImageLoader.getInstance().displayImage(str, im, op);
        //清除缓存
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ImageLoader.getInstance().clearDiskCache();

                Toast.makeText(MainActivity.this, "清除成功", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
